package cn.zhz.filter;

import cn.zhz.dao.HibernateUtil;
import org.hibernate.Transaction;

import javax.servlet.*;
import java.io.IOException;

public class OpenSessionInViewFilter implements Filter {

    public void init(FilterConfig filterConfigoha) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        Transaction tx = null;

        try {

            tx = HibernateUtil.currentSession().beginTransaction();

            filterChain.doFilter(servletRequest,servletResponse);

            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null){
                tx.rollback();
            }
        }
    }
    public void destroy() {

    }
}
